using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    public static SceneLoader Instance { get; private set; }

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject);
    }

    public void TransitionToScene(string sceneName)
    {
        StartCoroutine(TransitionCoroutine(sceneName));
    }

    // 切换场景的协程
    public IEnumerator TransitionCoroutine(string newSceneName)
    {
        // TODO加载前操作
        GameManager.Instance.Save();

        // 淡出当前场景
        yield return StartCoroutine(ScreenFader.Instance.FadeSceneOut());

        //清除 DOTween 库中当前正在进行的所有动画和补间
        DOTween.Clear();
        
        // 异步加载新场景
        yield return SceneManager.LoadSceneAsync(newSceneName);

        // TODO加载完成操作
        GameManager.Instance.Load();

        // 淡入新场景
        yield return StartCoroutine(ScreenFader.Instance.FadeSceneIn());
    }
}
